package com.android.camera2.common.ui;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyScrollView extends HorizontalScrollView implements View.OnClickListener {
    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private OnScrollListenerForUser onScrollListener;
    private OnScrollerItemClcikListener onScrollerItemClcikListener;
    private static final String TAG = MyScrollView.class.getSimpleName();

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount() - 1);
        // 如果为0，证明滑动到最左边
        if (view.getLeft() - getScrollX() == 0) {
            onScrollListener.onLeft();
            // 如果为0证明滑动到最右边
        } else if ((view.getRight() - (getWidth() + getScrollX())) == 0) {
            onScrollListener.onRight();
            // 说明在中间
        } else {
            onScrollListener.onScroll();
        }
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    public void onClick(View v) {
        int index = (int) v.getTag();
//        int position = getChildCenterPosition(index);
//        LogUtil.e(TAG,"index = " + index + "-----position = " + position );
        onScrollerItemClcikListener.onItemClick(v, index);
    }


    public interface OnScrollListenerForUser {
        void onRight();

        void onLeft();

        void onScroll();
    }

    public interface OnScrollerItemClcikListener {
        void onItemClick(View v, int position);
    }

    public void setOnScrollerItemClcikListener(OnScrollerItemClcikListener listener) {
        onScrollerItemClcikListener = listener;
    }

    public void setOnScrollListener(OnScrollListenerForUser onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    private String[] arr = new String[]{" photo", "video "};

    public void initScrollChildView(LinearLayout layout) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < arr.length; i++) {
            TextView sort = new TextView(getContext());
            sort.setText(arr[i]);
            sort.setId(i);
            sort.setTag(i);
            sort.setTextSize(15);
            sort.setOnClickListener(this);
            sort.setTextColor(Color.WHITE);
            sort.setMinHeight(30);
            sort.setPadding(20, 5, 20, 5);
            layout.addView(sort, i, params);

        }
    }

    /**
     * get item X
     *
     * @param
     * @return
     */
    private int getChildCenterPosition(int modeIndex) {
        if (getChildCount() <= 0) {
            return 0;
        }
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        if (viewGroup == null || viewGroup.getChildCount() == 0) {
            return 0;
        }
        int offset_tmp = 0;
        boolean isRTL = (View.LAYOUT_DIRECTION_RTL == this.getResources().getConfiguration().getLayoutDirection());
        int groupChildCount = viewGroup.getChildCount();
        int indexInParent = isRTL ? (groupChildCount - 2/**/) - modeIndex : modeIndex + 1;
        int numCompute = indexInParent + 1;

        int startComputeIndex = isRTL ? groupChildCount - 1 : 0;
        int secondComputeIndex = isRTL ? startComputeIndex - 1 : startComputeIndex + 1;
        int offset_target = 0;
        for (int i = 0; i < numCompute; i++) {
            View child = viewGroup.getChildAt(isRTL ? startComputeIndex - i : i);
            int child_width = child.getWidth();
            int childStartW = 0;
            int childSecondW = 0;
            if (0 == child_width) {
                int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                child.measure(w, h);
                child_width = child.getMeasuredWidth();
                childStartW = viewGroup.getChildAt(startComputeIndex).getMeasuredWidth();
                childSecondW = viewGroup.getChildAt(secondComputeIndex).getMeasuredWidth();
            } else {
                childStartW = viewGroup.getChildAt(startComputeIndex).getWidth();
                childSecondW = viewGroup.getChildAt(secondComputeIndex).getWidth();
            }
            offset_tmp = offset_tmp + child_width;
            if (i == indexInParent) {
                offset_target = offset_tmp - child_width / 2 - childStartW - childSecondW / 2;
                return offset_target;
            }
        }

        return 0;
    }

}
